﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OWANotifier.Properties;
using WinFormsUtil;
using System.Threading;

namespace OWANotifier
{
    public partial class NewMessageForm : Form
    {
        private int waitToRoll = 10;
        private int waitToClose = 10;

        public ICollection<Message> Messages { get; set; }

        private static object formLock = new object();

        SimpleTimer st = new SimpleTimer();


        private static NewMessageForm instance;

        public static NewMessageForm Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new NewMessageForm();
                }
                return NewMessageForm.instance;
            }
        }

        public static NewMessageForm NewInstance
        {
            get
            {
                lock (formLock)
                {
                    if (instance != null)
                    {
                        while (instance.FormToaster.Appearing || instance.FormToaster.Disappearing)
                        {
                            Thread.CurrentThread.Priority = ThreadPriority.Lowest;
                            Thread.Sleep(20);
                            Application.DoEvents();
                        }
                        if (instance.FormToaster.CompletelyShown)
                            instance.Close();
                        Thread.CurrentThread.Priority = ThreadPriority.Normal;
                        instance = null;
                    }
                }
                return Instance;
            }
        }


        private NewMessageForm()
        {
            InitializeComponent();
        }

        private void NewMessageForm_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = Messages;

            lnkNext.Enabled = bindingSource.Position < bindingSource.Count - 1;
            lnkPrev.Enabled = bindingSource.Position > 0;

            waitToRoll = 1;
            waitToClose = bindingSource.Count + 2;

            st.Interval = 2000;
            st.Tick += new EventHandler(st_Tick);
            st.Start();

        }

        void st_Tick(object sender, EventArgs e)
        {
            if (this.FormToaster.CompletelyShown)
            {
                if (waitToClose > 0)
                    waitToClose--;
                if (waitToClose == 0)
                    Close();
                else
                    if (waitToRoll > 0)
                        waitToRoll--;
                    else if (waitToRoll == 0)
                        if (bindingSource.Position >= bindingSource.Count - 1)
                        {
                            bindingSource.MoveFirst();
                        }
                        else
                        {
                            bindingSource.MoveNext();
                        }
            }

        }

        private void lnkOpen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!string.IsNullOrEmpty(lnkOpen.Tag as string))
            {
                if (lnkOpen.Tag.ToString() == "{Exception}")
                {
                    SystemMessage m = bindingSource.Current as SystemMessage;
                    if ((m != null) && (m.Exception != null))
                    {
                        MessageBox.Show(m.Exception.ToString(), m.Exception.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    System.Diagnostics.Process.Start(lnkOpen.Tag.ToString());
                }
            }
        }

        private void NewMessageForm_DoubleClick(object sender, EventArgs e)
        {
            Close();
        }

        private void lnkPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!FormToaster.Disappearing)
            {
                waitToClose = 10;
                waitToRoll = 10;
                bindingSource.MovePrevious();
            }
        }

        private void lnkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!FormToaster.Disappearing)
            {
                waitToClose = 10;
                waitToRoll = 10;
                bindingSource.MoveNext();
            }
        }

        private void bindingSource_PositionChanged(object sender, EventArgs e)
        {
            lblCount.Text = string.Format("{0} of {1}", bindingSource.Position + 1, bindingSource.Count);
            lnkNext.Enabled = bindingSource.Position < bindingSource.Count - 1;
            lnkPrev.Enabled = bindingSource.Position > 0;


        }

        private void pic_Click(object sender, EventArgs e)
        {

        }

        private void bindingSource_DataSourceChanged(object sender, EventArgs e)
        {
            if ((Messages.Count == 0) ||
                ((Messages.Count == 1) &&
                 (!Messages.ElementAt(0).Unread)))
            {
                pic.Image = Properties.Resources.envelopes_nounread;
            }
            else
            {
                pic.Image = Properties.Resources.envelopes;
            }
        }
    }
}
